<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    function observe(obj){
        if(typeof obj == "object"){
            for(let key in obj){
               defineReactive(obj,key,obj[key]); 
            }
        }
    }
    function defineReactive(data, key, value){
        observe(value);//递归劫持
        Object.defineProperty(data,key,{
            get(){
                return value;
            },
            set(val){
                observe(value);
                if(val!=value){
                    value = val;
                }    
            }
        })

    }
    function $set(data, key, value) {
        if (Array.isArray(data)) {
            data.splice(key, 1, value);
        } else {
            defineReactive(data, key, value);
        }
    }
    // let obj = [1,2,3];
    // $set(obj,0,100); 
    // console.log(obj);
    let obj = {};
    $set(obj, "name", {age:10});
    // console.log(obj.name.age);
    // obj.name.age = 20;
    // console.log(obj.name);
</script>